<?php
/**
 * Basic util functions.
 *
 * @author    Greg Sherwood <gsherwood@squiz.net>
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 */

namespace PHP_CodeSniffer\Util;

class Common
{
    /**
     * An array of variable types for param/var we will check.
     *
     * @var string[]
     */
    public static $allowedTypes = [
        'array',
        'boolean',
        'float',
        'integer',
        'mixed',
        'object',
        'string',
        'resource',
        'callable',
    ];

    /**
     * Return TRUE if the path is a PHAR file.
     *
     * @param string $path The path to use.
     *
     * @return mixed
     */
    public static function isPharFile($path)
    {
        if (0 === strpos($path, 'phar://')) {
            return true;
        }

        return false;
    }

    //end isPharFile()

    /**
     * Checks if a file is readable.
     *
     * Addresses PHP bug related to reading files from network drives on Windows.
     * e.g. when using WSL2.
     *
     * @param string $path The path to the file.
     *
     * @return bool
     */
    public static function isReadable($path)
    {
        if (true === is_readable($path)) {
            return true;
        }

        if (true === file_exists($path) && true === is_file($path)) {
            $f = @fopen($path, 'r');
            if (true === fclose($f)) {
                return true;
            }
        }

        return false;
    }

    //end isReadable()

    /**
     * CodeSniffer alternative for realpath.
     *
     * Allows for PHAR support.
     *
     * @param string $path The path to use.
     *
     * @return mixed
     */
    public static function realpath($path)
    {
        // Support the path replacement of ~ with the user's home directory.
        if ('~/' === substr($path, 0, 2)) {
            $homeDir = getenv('HOME');
            if (false !== $homeDir) {
                $path = $homeDir . substr($path, 1);
            }
        }

        // Check for process substitution.
        if (0 === strpos($path, '/dev/fd')) {
            return str_replace('/dev/fd', 'php://fd', $path);
        }

        // No extra work needed if this is not a phar file.
        if (false === self::isPharFile($path)) {
            return realpath($path);
        }

        // Before trying to break down the file path,
        // check if it exists first because it will mostly not
        // change after running the below code.
        if (true === file_exists($path)) {
            return $path;
        }

        $phar = \Phar::running(false);
        $extra = str_replace('phar://' . $phar, '', $path);
        $path = realpath($phar);
        if (false === $path) {
            return false;
        }

        $path = 'phar://' . $path . $extra;
        if (true === file_exists($path)) {
            return $path;
        }

        return false;
    }

    //end realpath()

    /**
     * Removes a base path from the front of a file path.
     *
     * @param string $path     The path of the file.
     * @param string $basepath The base path to remove. This should not end
     *                         with a directory separator.
     *
     * @return string
     */
    public static function stripBasepath($path, $basepath)
    {
        if (true === empty($basepath)) {
            return $path;
        }

        $basepathLen = \strlen($basepath);
        if (substr($path, 0, $basepathLen) === $basepath) {
            $path = substr($path, $basepathLen);
        }

        $path = ltrim($path, \DIRECTORY_SEPARATOR);
        if ('' === $path) {
            $path = '.';
        }

        return $path;
    }

    //end stripBasepath()

    /**
     * Detects the EOL character being used in a string.
     *
     * @param string $contents The contents to check.
     *
     * @return string
     */
    public static function detectLineEndings($contents)
    {
        if (1 !== preg_match("/\r\n?|\n/", $contents, $matches)) {
            // Assume there are no newlines.
            $eolChar = "\n";
        } else {
            $eolChar = $matches[0];
        }

        return $eolChar;
    }

    //end detectLineEndings()

    /**
     * Check if STDIN is a TTY.
     *
     * @return bool
     */
    public static function isStdinATTY()
    {
        // The check is slow (especially calling `tty`) so we static
        // cache the result.
        static $isTTY = null;

        if (null !== $isTTY) {
            return $isTTY;
        }

        if (false === \defined('STDIN')) {
            return false;
        }

        // If PHP has the POSIX extensions we will use them.
        if (true === \function_exists('posix_isatty')) {
            $isTTY = (true === posix_isatty(STDIN));

            return $isTTY;
        }

        // Next try is detecting whether we have `tty` installed and use that.
        if (true === \defined('PHP_WINDOWS_VERSION_PLATFORM')) {
            $devnull = 'NUL';
            $which = 'where';
        } else {
            $devnull = '/dev/null';
            $which = 'which';
        }

        $tty = trim(shell_exec("{$which} tty 2> {$devnull}"));
        if (false === empty($tty)) {
            exec("tty -s 2> {$devnull}", $output, $returnValue);
            $isTTY = (0 === $returnValue);

            return $isTTY;
        }

        // Finally we will use fstat.  The solution borrowed from
        // https://stackoverflow.com/questions/11327367/detect-if-a-php-script-is-being-run-interactively-or-not
        // This doesn't work on Mingw/Cygwin/... using Mintty but they
        // have `tty` installed.
        $type = [
            'S_IFMT' => 0170000,
            'S_IFIFO' => 0010000,
        ];

        $stat = fstat(STDIN);
        $mode = ($stat['mode'] & $type['S_IFMT']);
        $isTTY = ($mode !== $type['S_IFIFO']);

        return $isTTY;
    }

    //end isStdinATTY()

    /**
     * Prepares token content for output to screen.
     *
     * Replaces invisible characters so they are visible. On non-Windows
     * operating systems it will also colour the invisible characters.
     *
     * @param string   $content The content to prepare.
     * @param string[] $exclude A list of characters to leave invisible.
     *                          Can contain \r, \n, \t and a space.
     *
     * @return string
     */
    public static function prepareForOutput($content, $exclude = [])
    {
        if ('WIN' === strtoupper(substr(PHP_OS, 0, 3))) {
            if (false === \in_array("\r", $exclude, true)) {
                $content = str_replace("\r", '\r', $content);
            }

            if (false === \in_array("\n", $exclude, true)) {
                $content = str_replace("\n", '\n', $content);
            }

            if (false === \in_array("\t", $exclude, true)) {
                $content = str_replace("\t", '\t', $content);
            }
        } else {
            if (false === \in_array("\r", $exclude, true)) {
                $content = str_replace("\r", "\033[30;1m\\r\033[0m", $content);
            }

            if (false === \in_array("\n", $exclude, true)) {
                $content = str_replace("\n", "\033[30;1m\\n\033[0m", $content);
            }

            if (false === \in_array("\t", $exclude, true)) {
                $content = str_replace("\t", "\033[30;1m\\t\033[0m", $content);
            }

            if (false === \in_array(' ', $exclude, true)) {
                $content = str_replace(' ', "\033[30;1m·\033[0m", $content);
            }
        }//end if

        return $content;
    }

    //end prepareForOutput()

    /**
     * Returns true if the specified string is in the camel caps format.
     *
     * @param string $string      The string the verify.
     * @param bool   $classFormat If true, check to see if the string is in the
     *                            class format. Class format strings must start
     *                            with a capital letter and contain no
     *                            underscores.
     * @param bool   $public      If true, the first character in the string
     *                            must be an a-z character. If false, the
     *                            character must be an underscore. This
     *                            argument is only applicable if $classFormat
     *                            is false.
     * @param bool   $strict      If true, the string must not have two capital
     *                            letters next to each other. If false, a
     *                            relaxed camel caps policy is used to allow
     *                            for acronyms.
     *
     * @return bool
     */
    public static function isCamelCaps(
        $string,
        $classFormat = false,
        $public = true,
        $strict = true
    ) {
        // Check the first character first.
        if (false === $classFormat) {
            $legalFirstChar = '';
            if (false === $public) {
                $legalFirstChar = '[_]';
            }

            if (false === $strict) {
                // Can either start with a lowercase letter, or multiple uppercase
                // in a row, representing an acronym.
                $legalFirstChar .= '([A-Z]{2,}|[a-z])';
            } else {
                $legalFirstChar .= '[a-z]';
            }
        } else {
            $legalFirstChar = '[A-Z]';
        }

        if (0 === preg_match("/^{$legalFirstChar}/", $string)) {
            return false;
        }

        // Check that the name only contains legal characters.
        $legalChars = 'a-zA-Z0-9';
        if (preg_match("|[^{$legalChars}]|", substr($string, 1)) > 0) {
            return false;
        }

        if (true === $strict) {
            // Check that there are not two capital letters next to each other.
            $length = \strlen($string);
            $lastCharWasCaps = $classFormat;

            for ($i = 1; $i < $length; ++$i) {
                $ascii = \ord($string[$i]);
                if ($ascii >= 48 && $ascii <= 57) {
                    // The character is a number, so it cant be a capital.
                    $isCaps = false;
                } else {
                    if (strtoupper($string[$i]) === $string[$i]) {
                        $isCaps = true;
                    } else {
                        $isCaps = false;
                    }
                }

                if (true === $isCaps && true === $lastCharWasCaps) {
                    return false;
                }

                $lastCharWasCaps = $isCaps;
            }
        }//end if

        return true;
    }

    //end isCamelCaps()

    /**
     * Returns true if the specified string is in the underscore caps format.
     *
     * @param string $string The string to verify.
     *
     * @return bool
     */
    public static function isUnderscoreName($string)
    {
        // If there are space in the name, it can't be valid.
        if (false !== strpos($string, ' ')) {
            return false;
        }

        $validName = true;
        $nameBits = explode('_', $string);

        if (0 === preg_match('|^[A-Z]|', $string)) {
            // Name does not begin with a capital letter.
            $validName = false;
        } else {
            foreach ($nameBits as $bit) {
                if ('' === $bit) {
                    continue;
                }

                if ($bit[0] !== strtoupper($bit[0])) {
                    $validName = false;

                    break;
                }
            }
        }

        return $validName;
    }

    //end isUnderscoreName()

    /**
     * Returns a valid variable type for param/var tags.
     *
     * If type is not one of the standard types, it must be a custom type.
     * Returns the correct type name suggestion if type name is invalid.
     *
     * @param string $varType The variable type to process.
     *
     * @return string
     */
    public static function suggestType($varType)
    {
        if ('' === $varType) {
            return '';
        }

        if (true === \in_array($varType, self::$allowedTypes, true)) {
            return $varType;
        }
        $lowerVarType = strtolower($varType);
        switch ($lowerVarType) {
            case 'bool':
            case 'boolean':
                return 'boolean';
            case 'double':
            case 'real':
            case 'float':
                return 'float';
            case 'int':
            case 'integer':
                return 'integer';
            case 'array()':
            case 'array':
                return 'array';
            }//end switch

        if (false !== strpos($lowerVarType, 'array(')) {
            // Valid array declaration:
            // array, array(type), array(type1 => type2).
            $matches = [];
            $pattern = '/^array\(\s*([^\s^=^>]*)(\s*=>\s*(.*))?\s*\)/i';
            if (0 !== preg_match($pattern, $varType, $matches)) {
                $type1 = '';
                if (true === isset($matches[1])) {
                    $type1 = $matches[1];
                }

                $type2 = '';
                if (true === isset($matches[3])) {
                    $type2 = $matches[3];
                }

                $type1 = self::suggestType($type1);
                $type2 = self::suggestType($type2);
                if ('' !== $type2) {
                    $type2 = ' => ' . $type2;
                }

                return "array({$type1}{$type2})";
            }

            return 'array';
        //end if
        } elseif (true === \in_array($lowerVarType, self::$allowedTypes, true)) {
            // A valid type, but not lower cased.
            return $lowerVarType;
        } else {
            // Must be a custom type name.
            return $varType;
        }//end if
        //end if
    }

    //end suggestType()

    /**
     * Given a sniff class name, returns the code for the sniff.
     *
     * @param string $sniffClass The fully qualified sniff class name.
     *
     * @return string
     */
    public static function getSniffCode($sniffClass)
    {
        $parts = explode('\\', $sniffClass);
        $sniff = array_pop($parts);

        if ('Sniff' === substr($sniff, -5)) {
            // Sniff class name.
            $sniff = substr($sniff, 0, -5);
        } else {
            // Unit test class name.
            $sniff = substr($sniff, 0, -8);
        }

        $category = array_pop($parts);
        $sniffDir = array_pop($parts);
        $standard = array_pop($parts);

        return $standard . '.' . $category . '.' . $sniff;
    }

    //end getSniffCode()

    /**
     * Removes project-specific information from a sniff class name.
     *
     * @param string $sniffClass The fully qualified sniff class name.
     *
     * @return string
     */
    public static function cleanSniffClass($sniffClass)
    {
        $newName = strtolower($sniffClass);

        $sniffPos = strrpos($newName, '\sniffs\\');
        if (false === $sniffPos) {
            // Nothing we can do as it isn't in a known format.
            return $newName;
        }

        $end = (\strlen($newName) - $sniffPos + 1);
        $start = strrpos($newName, '\\', ($end * -1));

        if (false === $start) {
            // Nothing needs to be cleaned.
            return $newName;
        }

        return substr($newName, ($start + 1));
    }

    //end cleanSniffClass()
}//end class
